package com.codefish.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.entity.InMoney;
import com.codefish.entity.SysUserTokenEntity;
import com.codefish.service.InMoneyService;
import com.codefish.util.Assert;
import com.codefish.util.JWTService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * 收入
 *
 * @author codefish
 * @email cool_fish@aliyun.com
 * @date 2021-07-12 14:43:11
 */
@RestController
@RequestMapping("/inMoney")
public class InMoneyController {

    /**
     * 收入
     */
    @Autowired
    private InMoneyService inMoneyService;


    /**
     * 新增单个
     *
     * @param token
     * @param inMoney
     * @return
     */
    @PostMapping("/saveOne")
    public R saveOne(@RequestHeader("Authentication") String token
            , @RequestBody InMoney inMoney) {
        Assert.isNull(token, "token不可为空");
        Assert.isNull(inMoney.getInMoneyType(), "inMoneyType不可为空");
        Assert.isNull(inMoney.getInDate(), "inDate不可为空");
        Assert.isNull(inMoney.getCapitalAccountId(), "capitalAccountId不可为空");
        Assert.isNull(inMoney.getInAmount(), "inAmount不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前家庭无效");

        // set create_id /create_time/ familyId
        inMoney.setCreateId(user.getId());
        inMoney.setCreateTime(LocalDateTime.now());
        inMoney.setFamilyId(user.getFamilyId());

        Integer id = inMoneyService.saveOne(inMoney);

        if (ObjectUtil.isEmpty(id)) {
            return R.error("新增失败");
        }

        return R.ok("新增成功").put("id", id);
    }


    /**
     * 修改单个
     *
     * @param token
     * @param inMoney
     * @return
     */
    @PostMapping("/updateOne")
    public R updateOne(@RequestHeader("Authentication") String token
            , @RequestBody InMoney inMoney) {

        Assert.isNull(token, "token不可为空");
        Assert.isNull(inMoney.getId(), "id不可为空");
        Assert.isNull(inMoney.getInMoneyType(), "inMoneyType不可为空");
        Assert.isNull(inMoney.getInDate(), "inDate不可为空");
        Assert.isNull(inMoney.getCapitalAccountId(), "capitalAccountId不可为空");
        Assert.isNull(inMoney.getInAmount(), "inAmount不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getId(), "当前用户无效");
        Assert.isNull(user.getFamilyId(), "当前家庭无效");

        // set create_id /create_time/ familyId
        inMoney.setUpdateId(user.getId());
        inMoney.setUpdateTime(LocalDateTime.now());

        boolean flag = inMoneyService.updateOne(inMoney);

        if (flag) {
            return R.ok("修改成功");
        }

        return R.error("修改失败");
    }

    /**
     * 逻辑删除单个
     *
     * @param id
     * @return 是否删除成功
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestHeader("Authentication") String token
            , @RequestParam("id") Integer id) {

        boolean flag = inMoneyService.deleteOne(id);
        if (flag) {
            return R.ok("删除成功");
        }

        return R.error("删除失败");
    }

    /**
     * 条件分页查询
     *
     * @param token
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getPageByParams")
    public R getPageByParams(@RequestHeader("Authentication") String token
            , @RequestParam("pageNo") Integer pageNo
            , @RequestParam("pageSize") Integer pageSize) {

        Assert.isNull(token, "token不可为空");
        Assert.isNull(pageNo, "pageNo不可为空");
        Assert.isNull(pageSize, "pageSize不可为空");

        SysUserTokenEntity user = JWTService.getUser(token);
        Assert.isNull(user.getFamilyId(), "当前用户家庭信息无效");

        IPage<InMoney> retPage = inMoneyService.getPageByParams(pageNo, pageSize, user.getFamilyId());

        return R.ok("查询成功").put("data",retPage);
    }

}
